----------------------
-- Object Container class
----------------------
ObjectContainer = {}
ObjectContainer.__index = ObjectContainer

function ObjectContainer.new(id, x, y, w, h)
	local o = {}
	setmetatable(o, ObjectContainer)
	
	-- variables
    o.id = id
    o.clamp_area = Rectangle.new(x, y, w, h)
	o.objects = {} -- list of objects
	
	return o
end

-- get size of this container
function ObjectContainer:getSize()
	return #self.objects
end

-- get object
function ObjectContainer:getObject(id)
	return self.objects[id]
end

-- get object
function ObjectContainer:getObjectByType(type)
    local obj = {}
    if #self.objects == 0 then return obj end
    
    for i = 1, #self.objects do
        if self.objects[i].c_type == type then
            table.insert(obj, self.objects[i])
        end
    end
    
    return obj
end

-- get first object
function ObjectContainer:getFirstObjectByType(type)
	if #self.objects == 0 then return nil end
	
	for i = 1, #self.objects do
		if self.objects[i].c_type == type then
			return self.objects[i]
		end
	end
end


-- add object
function ObjectContainer:addObject(object)
	table.insert(self.objects, object)
end

-- remove object
function ObjectContainer:removeObject(id)
	table.remove(self.objects, id)
end

-- init objects in case of reinitialize
function ObjectContainer:init(prev_id)
    if #self.objects == 0 then return end
	
	for i = 1, #self.objects do 
		if self.objects[i] ~= nil then
			self.objects[i]:init(prev_id)
		end
	end
end

-- update objects
function ObjectContainer:update()
	if #self.objects == 0 then return end
	
	for i = 1, #self.objects do 
		if self.objects[i] ~= nil then
			self.objects[i]:update()
		end
	end
end

-- draw objects
function ObjectContainer:draw(camera)
	if #self.objects == 0 then return end
	
	for i = 1, #self.objects do 
		if self.objects[i] ~= nil then
			self.objects[i]:draw(camera)
		end
	end
end

-- draw objects effects
function ObjectContainer:drawEffects(camera)
	if #self.objects == 0 then return end
	
	for i = 1, #self.objects do 
		if self.objects[i] ~= nil then
			self.objects[i]:drawEffects(camera)
		end
	end
end